package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.ohos.afkt.ResourceTable;
import com.dev.utils.app.toast.ToastTintUtils;
import com.dev.widget.ui.LoadProgressBar;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

public class ProgressBarActivity extends BaseActivity {

    private static final String TAG = ProgressBarActivity.class.getSimpleName();

    private AbilitySlice mSlice = this;

    private LoadProgressBar progressBar1;

    private LoadProgressBar progressBar2;
    private LoadProgressBar progressBar3;
    private LoadProgressBar progressBar4;

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        Text text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        text.setText("ViewPager 滑动监听、控制滑动");
        bar_Layout.addComponent(bar);
    }

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_progressbar;
    }

    @Override
    protected void initView() {
        initValue();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    @Override
    protected void initListener() {

        select_retuen.setClickedListener(component -> {
            // LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });
    }

    private MyHandler myHandler = new MyHandler(EventRunner.create());
    private void initValue() {
        progressBar1 = (LoadProgressBar) findComponentById(ResourceTable.Id_vid_ap_bar1);
        progressBar2 = (LoadProgressBar) findComponentById(ResourceTable.Id_vid_ap_bar2);
        progressBar3 = (LoadProgressBar) findComponentById(ResourceTable.Id_vid_ap_bar3);
        progressBar4 = (LoadProgressBar) findComponentById(ResourceTable.Id_vid_ap_bar4);
        progressBar1.setProgress(0);
        progressBar2.setProgress(0);
        progressBar3.setProgress(0);
        progressBar4.setProgress(0);

        myHandler.sendEvent(0, 100);
    }

    private class MyHandler extends EventHandler {
        public MyHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            try {

                int progress = progressBar1.getProgress() + 1;
                getMainTaskDispatcher().asyncDispatch(() -> {
                    progressBar1.setProgress(progress);
                    progressBar2.setProgress(progress);
                    progressBar3.setProgress(progress);
                    progressBar4.setProgress(progress);
                });


                if (progress < progressBar1.getMax()) {
                    myHandler.sendEvent(0, 100);
                }
            } catch (Exception e) {
                ToastTintUtils.success("error =" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
